package com.bwie.parkdemo.mvvm.viewmodel;

import android.app.Application;
import android.support.annotation.NonNull;
import android.util.Log;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.bwie.lib_core.base.BaseEntity;
import com.bwie.lib_core.mvvm.base.BaseOBViewModel;
import com.bwie.parkdemo.mvvm.model.SysSignModel;
import com.bwie.parkdemo.mvvm.model.entity.SysSignEntity;
import com.google.gson.Gson;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 签到打卡ViewModel
 * {
 "endAddress": "string",
 "endTime": "2020-03-12T02:30:22.489Z",
 "startAddress": "string",
 "startTime": "2020-03-12T02:30:22.489Z",
 "userId": 0
 }
 * */
public class SysSignViewModel extends BaseOBViewModel<SysSignModel>{

    public SignClickListener clickListener;
    public String values;
    private int signId;
    private int userId;
    private String startTime;

    public SysSignViewModel(@NonNull Application application) {
        super(application);
        clickListener = new SignClickListener();
        signId = SPUtils.getInstance().getInt("signId",0);
        if (signId == 0){
            values = "签到";
        }else {
            values = "签退";
            startTime = SPUtils.getInstance().getString("startTime");
        }

    }

    @Override
    protected SysSignModel createModel() {
        return new SysSignModel();
    }

    @Override
    protected void initData() {
        userId = m.requestUser(getApplication()).uId;
    }

    @Override
    public void onNext(BaseEntity baseEntity) {
        SysSignEntity entity = (SysSignEntity) baseEntity;
        //签到成功后需要保存->当前此次签到的id以及开始时间
        if (entity.statuesCode.equals("200")) {
            SPUtils.getInstance().put("signId", entity.getData().signId);
            SPUtils.getInstance().put("startTime", startTime);
            showMsg("签到成功");
            finishAct();
        }
    }

    //签到方法
    private void startSign(){
        startTime = getTime();
        Map<String,Object>map = new HashMap<>();
        map.put("userId",userId);
        map.put("endAddress","签退经纬度");
        map.put("startAddress","签到经纬度");
        map.put("startTime",startTime);
        map.put("endTime",startTime);
        String jsonStr = new Gson().toJson(map);
        Log.e("ZXWQ","startSign:"+jsonStr);
        RequestBody body = RequestBody.create(MediaType.parse("application/json"),jsonStr);
        m.request(body).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    //签退方法
    private void endSign(){
        Map<String,Object>map = new HashMap<>();
        map.put("userId",userId);
        map.put("signId",signId);
        map.put("endAddress","签退经纬度");
        map.put("startAddress","签到经纬度");
        map.put("startTime",startTime);
        map.put("endTime",getTime());
        String jsonStr = new Gson().toJson(map);
        Log.e("ZXWQ","startSign:"+jsonStr);
        RequestBody body = RequestBody.create(MediaType.parse("application/json"),jsonStr);
        m.requestEndSign(body).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                        showDialog();
                    }

                    @Override
                    public void onNext(BaseEntity baseEntity) {
                        SysSignEntity entity = (SysSignEntity) baseEntity;
                        if (entity.statuesCode.equals("200")){
                            SPUtils.getInstance().put("signId",0);
                            SPUtils.getInstance().put("startTime","");
                            showMsg("签退成功");
                            finishAct();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        showMsg(e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        disDialog();
                    }
                });
    }

    private String getTime(){
        long time = System.currentTimeMillis();
        String punchTime = TimeUtils.millis2String(time,new SimpleDateFormat("yyyy-MM-dd"));
        punchTime +="T";
        punchTime = punchTime+TimeUtils.millis2String(time,new SimpleDateFormat("hh:mm:ss"));
        punchTime +="Z";
        return punchTime;
    }

    //按钮点击监听器
    public class SignClickListener{
        public void signClick(){
            //打卡按钮点击
            if (signId==0){
                startSign();
            }else
                endSign();
        }
    }

}
